import 'package:lib_core/lib_core.dart';

import '../api/user_client.dart';

class UserLogic extends GetxController {
  final _client = UserClient();
  final Rx<UserModel?> user = Rx(null);

  Future<UserModel?> login(String username, String password) async {
    Loading.show();
    final user = await _client.loginPwd(username, password).tryAwait((error) => error.showError);
    Loading.dismiss();
    if (user != null) {
      AuthService.to.saveUser(user);
    }
    return user;
  }

  Future<UserModel?> register(String username, String password, String passwordAgain) async {
    Loading.show();
    final user = await _client.register(username, password, password).tryAwait((error) => error.showError);
    Loading.dismiss();
    if (user != null) {
      AuthService.to.saveUser(user);
    }
    return user;
  }

  Future<void> logout() async {
    Loading.show();
    await _client.logout();
    Loading.dismiss();
  }
}
